package com.qbccn.userservice.controller;

import com.qbccn.dto.CodeStatus;
import com.qbccn.dto.ResponseResult;
import com.qbccn.dto.ResponseResultPage;
import com.qbccn.userservice.dto.AddMenuParam;
import com.qbccn.userservice.dto.UpdateMenuParam;
import com.qbccn.userservice.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linke
 * @date 2020-03-09 下午 21:30
 * @description
 */
@RestController
@RequestMapping("/user/sm")
class UserSMController {

    @Autowired
    private UserService userService;

    @GetMapping("findList")
    public ResponseResultPage findList(String username, String systemCode, String comCode,
                                       Integer page, Integer size) {
        if (page == null)
            page = 1;
        if (size == null)
            size = 10;
        else if (size > 50)
            size = 50;
        if (StringUtils.isEmpty(username))
            username = null;
        if (StringUtils.isEmpty(systemCode))
            systemCode = null;
        if (StringUtils.isEmpty(comCode))
            comCode = null;
        return userService.findList(username, systemCode, comCode, page, size);
    }

    @GetMapping("getSystemMenu/{systemCode}")
    public ResponseResult getSystemMenu(@PathVariable("systemCode") String systemCode) {
        if (StringUtils.isEmpty(systemCode))
            systemCode = "sso";
        return userService.getSystemMenus(systemCode);
    }

    @PostMapping("addMenu")
    public ResponseResult addMenu(AddMenuParam addMenuParam) {
        if (StringUtils.isEmpty(addMenuParam.getPath())) {
            return new ResponseResult(CodeStatus.FAIL, "路径不能为空！");
        } else if (StringUtils.isEmpty(addMenuParam.getName())) {
            return new ResponseResult(CodeStatus.FAIL, "名称不能为空！");
        } else if (StringUtils.isEmpty(addMenuParam.getSystemCode())) {
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        } else if (StringUtils.isEmpty(addMenuParam.getTitle())) {
            return new ResponseResult(CodeStatus.FAIL, "标题不能为空！");
        }
        return userService.addMenuParam(addMenuParam);
    }

    @GetMapping("getRoleList")
    public ResponseResultPage getRoleList(String systemCode, int page, int size) {
        return userService.getRoleList(systemCode, page, size);
    }

    @GetMapping("getRoles")
    public ResponseResultPage getRoles(String systemCode, Integer userId) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResultPage(CodeStatus.FAIL, "系统码不能为空！");
        return userService.getRoles(systemCode, userId);
    }

    @PostMapping("updateUserRole")
    public ResponseResult updateUserRole(String systemCode, Integer userId,
                                         String roleIds, String status) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        if (StringUtils.isEmpty(roleIds) || StringUtils.isEmpty(status))
            return new ResponseResult(CodeStatus.FAIL, "roleIds或status入参有误！格式：roleIds=status=1,2,3");
        String[] sr = roleIds.split(",");
        String[] ss = status.split(",");
        if (sr.length != ss.length)
            return new ResponseResult(CodeStatus.FAIL, "roleIds或status数组长度不一致！");
        int[] r = new int[sr.length];
        int[] s = new int[ss.length];
        for (int i = 0; i < sr.length; i++) {
            r[i] = Integer.valueOf(sr[i]);
            s[i] = Integer.valueOf(ss[i]);
        }
        return userService.updateRoles(systemCode, userId, r, s);
    }

    @PostMapping("addRole")
    public ResponseResult addRole(String systemCode, String roleName, String roleDesc) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        if (StringUtils.isEmpty(roleName))
            return new ResponseResult(CodeStatus.FAIL, "角色名不能为空！");
        if (StringUtils.isEmpty(roleDesc))
            return new ResponseResult(CodeStatus.FAIL, "描述不能为空！");
        if (roleName.indexOf("ROLE_" + systemCode.toUpperCase() + "_") == -1) {
            return new ResponseResult(CodeStatus.FAIL, "角色名称不正确，请使用 ROLE_系统码_名称 的全大写形式！");
        }
        return userService.addRole(systemCode, roleName, roleDesc);
    }

    @PostMapping("updateRole")
    public ResponseResult updateRole(int id, String systemCode, String roleName, String roleDesc) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        if (StringUtils.isEmpty(roleName))
            return new ResponseResult(CodeStatus.FAIL, "角色名不能为空！");
        if (StringUtils.isEmpty(roleDesc))
            return new ResponseResult(CodeStatus.FAIL, "描述不能为空！");
        if (roleName.indexOf("ROLE_" + systemCode.toUpperCase() + "_") == -1) {
            return new ResponseResult(CodeStatus.FAIL, "角色名称不正确，请使用 ROLE_系统码_名称 的全大写形式！");
        }
        return userService.updateRole(id, systemCode, roleName, roleDesc);
    }

    @GetMapping("getMenus")
    public ResponseResult getMenus(String systemCode, String username) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        if (StringUtils.isEmpty(username))
            return new ResponseResult(CodeStatus.FAIL, "用户名不能为空！");
        return userService.getMenus(systemCode, username);
    }

    @PostMapping("updateUserMenu")
    public ResponseResult updateUserMenu(String systemCode, String username,
                                         String menuIds, String status, String parentIds) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        if (StringUtils.isEmpty(username))
            return new ResponseResult(CodeStatus.FAIL, "用户名不能为空！");
        if (StringUtils.isEmpty(menuIds) || StringUtils.isEmpty(status))
            return new ResponseResult(CodeStatus.FAIL, "roleIds或status入参有误！格式：roleIds=status=1,2,3");
        String[] sm = menuIds.split(",");
        String[] ss = status.split(",");
        if (sm.length != ss.length)
            return new ResponseResult(CodeStatus.FAIL, "roleIds或status数组长度不一致！");
        int[] m = new int[sm.length];
        int[] s = new int[ss.length];
        for (int i = 0; i < sm.length; i++) {
            m[i] = Integer.valueOf(sm[i]);
            s[i] = Integer.valueOf(ss[i]);
        }
        if (!StringUtils.isEmpty(parentIds)) {
            String[] sp = parentIds.split(",");
            int[] p = new int[sp.length];
            for (int i = 0; i < sp.length; i++)
                p[i] = Integer.valueOf(sp[i]);
            return userService.updateUserMenu(systemCode, username, m, s, p);
        }
        return userService.updateUserMenu(systemCode, username, m, s, new int[0]);
    }

    @PostMapping("deleteMenu")
    public ResponseResult deleteMenu(String systemCode, int menuId) {
        if (StringUtils.isEmpty(systemCode))
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        return userService.deleteMenu(systemCode, menuId);
    }

    @PostMapping("updateMenu")
    public ResponseResult updateMenu(UpdateMenuParam menuParam) {
        if (StringUtils.isEmpty(menuParam.getPath())) {
            return new ResponseResult(CodeStatus.FAIL, "路径不能为空！");
        } else if (StringUtils.isEmpty(menuParam.getName())) {
            return new ResponseResult(CodeStatus.FAIL, "名称不能为空！");
        } else if (StringUtils.isEmpty(menuParam.getSystemCode())) {
            return new ResponseResult(CodeStatus.FAIL, "系统码不能为空！");
        } else if (StringUtils.isEmpty(menuParam.getTitle())) {
            return new ResponseResult(CodeStatus.FAIL, "标题不能为空！");
        }
        if (!(menuParam.getSortId() <= 999 && menuParam.getSortId() >= 0))
            return new ResponseResult(CodeStatus.FAIL, "排序级别在0~999范围！");
        return userService.updateMenu(menuParam);
    }
}
